package cn.panda.edu.config.mybatis;

import cn.panda.edu.config.druid.DruidProperties;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Properties;

@Configuration
public class MybatisConfig implements TransactionManagementConfigurer {
	private Logger logger = LoggerFactory.getLogger(MybatisConfig.class);
	@Resource
	private DruidProperties druidProperties;

	// private static String MYBATIS_CONFIG = "mybatis-config.xml" ;
	private static final String TYPE_ALIAS_PACKAGE = "cn.panda.edu.model";
	// private static final String MAPPER_LOCATION = "mapping/*Mapper.xml" ;

	@Bean(destroyMethod = "close")
	public DruidDataSource druidDataSource() {

		DruidDataSource datasource = new DruidDataSource();

		datasource.setUrl(druidProperties.getUrl());
		datasource.setUsername(druidProperties.getUsername());
		datasource.setPassword(druidProperties.getPassword());
		datasource.setDriverClassName(druidProperties.getDriver());
		datasource.setInitialSize(druidProperties.getInitialSize());
		datasource.setMinIdle(druidProperties.getMinIdle());
		datasource.setMaxActive(druidProperties.getMaxActive());
		datasource.setMaxWait(druidProperties.getMaxWait());
		datasource.setTimeBetweenEvictionRunsMillis(druidProperties.getTimeBetweenEvictionRunsMillis());
		datasource.setMinEvictableIdleTimeMillis(druidProperties.getMinEvictableIdleTimeMillis());
		datasource.setValidationQuery(druidProperties.getValidationQuery());
		datasource.setTestWhileIdle(druidProperties.isTestWhileIdle());
		datasource.setTestOnBorrow(druidProperties.isTestOnBorrow());
		datasource.setTestOnReturn(druidProperties.isTestOnReturn());
		datasource.setPoolPreparedStatements(druidProperties.isPoolPreparedStatements());
		datasource.setRemoveAbandoned(druidProperties.isRemoveAbandoned());
		datasource.setLogAbandoned(druidProperties.isLogAbandoned());
		datasource.setRemoveAbandonedTimeout(druidProperties.getRemoveAbandonedTimeout());
		try {
			datasource.setFilters(druidProperties.getFilters());
		} catch (SQLException e) {
			logger.error("druid configuration initialization filter", e);
		}
		return datasource;
	}

	/**
	 * 制定使用的事物类型，即jdbc的DataSourceTransactionManager
	 */
	@Bean
	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(this.druidDataSource());
	}

	@Bean
	public SqlSessionFactory createSqlSessionFactoryBean() throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		// ResourcePatternResolver resolver = new
		// PathMatchingResourcePatternResolver();

		/** 设置mybatis configuration 扫描路径 */
		// sqlSessionFactoryBean.setConfigLocation(new
		// ClassPathResource(MYBATIS_CONFIG));
		/** 设置datasource */
		sqlSessionFactoryBean.setDataSource(this.druidDataSource());
		/** 设置typeAlias 包扫描路径 */
		sqlSessionFactoryBean.setTypeAliasesPackage(TYPE_ALIAS_PACKAGE);
		// sqlSessionFactoryBean.setMapperLocations(resolver.getResources(MAPPER_LOCATION));
		return sqlSessionFactoryBean.getObject();
	}

	@Bean
	public PageHelper pageHelper() {
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("offsetAsPageNum", "true");
		properties.setProperty("rowBoundsWithCount", "true");
		properties.setProperty("reasonable", "true");
		properties.setProperty("dialect", "mysql"); // 配置mysql数据库的方言
		pageHelper.setProperties(properties);
		return pageHelper;
	}

	@Bean
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
